package net.wofly.right.web;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.util.ReturnBean;
import net.wofly.common.web.BaseController;
import net.wofly.right.domain.ReceivingAddr;
import net.wofly.right.service.IReceivingAddrService;
import net.wofly.right.validator.UserValidator;

@Controller
@RequestMapping(value = "/api/user")
@Api(description = "客户收货地址")
public class ReceivingAddrController extends BaseController {


    @Resource
    IReceivingAddrService iReceivingAddrService;

    /**
     * @param receivingAddr
     */
    @ApiOperation(value = "添加收货地址")
    @ApiImplicitParams({@ApiImplicitParam(name = "receivingAddr", value = "收货地址实体", required = true, dataType = "ReceivingAddr")})
    @RequestMapping(value = "/addReceivingAddr", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<ReceivingAddr> addReceivingAddr(@RequestBody ReceivingAddr receivingAddr) {
        ReturnBean<ReceivingAddr> returnBean;
        String errorMessage = UserValidator.validateAddReceivingAddrParams(receivingAddr);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<ReceivingAddr>(-1, errorMessage);
        } else {
            returnBean = iReceivingAddrService.addReceivingAddr(receivingAddr);
        }
        return returnBean;
    }

    /**
     * @param receivingAddrID
     */
    @ApiOperation(value = "删除收货地址")
    @ApiImplicitParams({@ApiImplicitParam(name = "receivingAddrID", value = "收货地址ID", required = true, dataType = "String", paramType = "form")})
    @RequestMapping(value = "/delReceivingAddr", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<String> delReceivingAddr(String receivingAddrID) {
        ReturnBean<String> returnBean;
        String errorMessage = UserValidator.validateReceivingAddrID(receivingAddrID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<String>(-1, errorMessage);
        } else {
            returnBean = iReceivingAddrService.delReceivingAddr(receivingAddrID);
        }
        return returnBean;
    }

    /**
     * @param userID
     */
    @ApiOperation(value = "获取收货地址")
    @ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "客户ID", required = true, dataType = "String", paramType = "form")})
    @RequestMapping(value = "/getUserReceivingAddr", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<ReceivingAddr>> getUserReceivingAddr(String userID) {
        ReturnBean<List<ReceivingAddr>> returnBean;
        String errorMessage = UserValidator.validateUserID(userID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<List<ReceivingAddr>>(-1, errorMessage);
        } else {
            returnBean = iReceivingAddrService.getUserReceivingAddr(userID);
        }
        return returnBean;
    }

    /**
     * @param receivingAddr
     */
    @ApiOperation(value = "更新收货地址")
    @ApiImplicitParams({@ApiImplicitParam(name = "receivingAddr", value = "收货地址实体", required = true, dataType = "ReceivingAddr")})
    @RequestMapping(value = "/updateReceivingAddr", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<ReceivingAddr> updateReceivingAddr(@RequestBody ReceivingAddr receivingAddr) {
        ReturnBean<ReceivingAddr> returnBean;
        String errorMessage = UserValidator.validateUpdateReceivingAddrParams(receivingAddr);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<ReceivingAddr>(-1, errorMessage);
        } else {
            returnBean = iReceivingAddrService.updateReceivingAddr(receivingAddr);
        }
        return returnBean;
    }
    
    /**
     * @param receivingAddr
     */
    @ApiOperation(value = "设置默认收货地址")
    @ApiImplicitParams({@ApiImplicitParam(name = "receivingAddrID", value = "收货地址ID", required = true, dataType = "String", paramType = "form") ,
    						@ApiImplicitParam(name = "defaultFlag", value = "是否默认地址标志", required = true, dataType = "Integer", paramType = "form")})
    @RequestMapping(value = "/setDefaultReceivingAddr", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<ReceivingAddr> setDefaultReceivingAddr(String receivingAddrID ,Integer defaultFlag) {
        ReturnBean<ReceivingAddr> returnBean;
        String errorMessage = UserValidator.validateSetDefaultReceivingAddrParams(receivingAddrID ,defaultFlag);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<ReceivingAddr>(-1, errorMessage);
        } else {
            returnBean = iReceivingAddrService.setDefaultReceivingAddr(receivingAddrID, defaultFlag) ;
        }
        return returnBean;
    }
}
